home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Graphics⁄Sound / Fudd Source / Fudd ƒ / Fudd code ƒ / fudd progress.c < prev    next >
Text File  |  1994-02-06  |  4KB  |  194 lines

  1. /**********************************************************************\
  2.  
  3. File:        fudd progress.c
  4.  
  5. Purpose:    This module handles the progress bar and dealing with
  6.             events while the progress bar is up.
  7.             
  8.  
  9.  
  10. Fudd -=- convert text to Elmer Fudd talk
  11. Copyright ©1994, Mark Pilgrim
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program in a file named "GNU General Public License".
  25. If not, write to the Free Software Foundation, 675 Mass Ave,
  26. Cambridge, MA 02139, USA.
  27.  
  28. \**********************************************************************/
  29.  
  30. #include "Power.h"
  31. #include "program globals.h"
  32. #include "fudd progress.h"
  33. #include "msg dialogs.h"
  34. #include "msg environment.h"
  35. #include "msg menus.h"
  36. #include "msg main.h"
  37.  
  38. enum
  39. {
  40.     progressDialogID = 210,
  41.     progressText = 1,
  42.     progressBar = 2
  43. };
  44.  
  45. static    DialogPtr        dlog;
  46. static    Rect            box;
  47. static    unsigned long    curProgress;
  48. static    unsigned long    maxProgress;
  49.  
  50. static pascal void DrawProgressBar(WindowPtr theWindow, int item);
  51.  
  52. static pascal void DrawProgressBar(WindowPtr theWindow, int item)
  53. {
  54.     Rect                tempBox;
  55.     unsigned long        length;
  56.     unsigned long        width;
  57.     long double            temp;
  58.     
  59.     SetPort(theWindow);
  60.     
  61.     FrameRect(&box);
  62.     
  63.     length = box.right - box.left;
  64.     
  65.     width = length * curProgress;
  66.     if((width / length) != curProgress) {
  67.         temp = ((long double)curProgress) / ((long double)maxProgress);
  68.         width = temp * length;
  69.     } else {
  70.         width /= maxProgress;
  71.     }
  72.     
  73.     tempBox = box;
  74.     InsetRect(&tempBox, 1, 1);
  75.     tempBox.left += width;
  76.     FillRect(&tempBox, ltGray);
  77.     
  78.     tempBox = box;
  79.     InsetRect(&tempBox, 1, 1);
  80.     tempBox.right = tempBox.left + width - 1;
  81.     ForeColor(cyanColor);
  82.     PaintRect(&tempBox);
  83.     ForeColor(blackColor);
  84. }
  85.  
  86. DialogPtr OpenProgressDialog(unsigned long max, Str255 theTitle)
  87. {
  88.     int                itemType;
  89.     Handle            itemH;
  90.     Rect            otherBox;
  91.     
  92.     PositionDialog('DLOG', progressDialogID);
  93.     dlog = GetNewDialog(progressDialogID, 0L, (WindowPtr)-1L);
  94.     if(dlog == 0L)
  95.         return 0L;
  96.     
  97.     GetDItem(dlog, progressBar, &itemType, &itemH, &box);
  98.     SetDItem(dlog, progressBar, userItem + itemDisable, DrawProgressBar, &box);
  99.     
  100.     curProgress = 0;
  101.     maxProgress = max;
  102.     
  103.     SetWTitle((WindowPtr)dlog, theTitle);
  104.     
  105.     ShowWindow(dlog);
  106.     DrawDialog(dlog);
  107.     
  108.     UpdateProgressDialog(0);
  109.     
  110.     gInProgress=TRUE;
  111.     AdjustMenus();
  112.     DrawMenuBar();
  113.     
  114.     return dlog;
  115. }
  116.  
  117. void SetProgressText(Str255 p1, Str255 p2, Str255 p3, Str255 p4)
  118. {
  119.     ParamText(p1, p2, p3, p4);
  120. }
  121.  
  122. void UpdateProgressDialog(unsigned long cur)
  123. {
  124.     curProgress = cur;
  125.     if(curProgress >= maxProgress)
  126.         curProgress = maxProgress-1;
  127.     
  128.     SetPort(dlog);
  129.     
  130.     DrawProgressBar(dlog, progressBar);
  131.     
  132.     if (gHasPowerManager)
  133.         IdleUpdate();
  134. }
  135.  
  136. void DismissProgressDialog(void)
  137. {
  138.     if (dlog!=0L)
  139.         DisposDialog(dlog);
  140.     dlog=0L;
  141.     gInProgress=FALSE;
  142.     AdjustMenus();
  143.     DrawMenuBar();
  144. }
  145.  
  146. #define TheCancelKey    '.'
  147.  
  148. Boolean DealWithOtherPeople(void)
  149. {
  150.     /* this is just a small useful function to see if the user has cancelled */
  151.     /* a lengthy operation with command-period; could come in handy, I suppose, */
  152.     /* in a somewhat bizarre set of circumstances... */
  153.     /* Note that this procedure will break under AUX */
  154.     /* Note also that this returns TRUE if there has been no attempt to cancel */
  155.     
  156.     Boolean            foundEvent;
  157.     EvQElPtr        eventQPtr;
  158.     QHdrPtr            eventQHdr;
  159.     char            thisChar;
  160.     long            isCmdKey;
  161.     EventRecord        event;
  162.     Boolean            notDoneYet;
  163.     
  164.     foundEvent=FALSE;
  165.     eventQHdr=GetEvQHdr();
  166.     eventQPtr=(EvQElPtr)(eventQHdr->qHead);
  167.     while ((eventQPtr!=0L) && (!foundEvent))
  168.     {
  169.         if (eventQPtr->evtQWhat==keyDown)
  170.         {
  171.             thisChar=(char)((eventQPtr->evtQMessage)&charCodeMask);
  172.             isCmdKey=(eventQPtr->evtQModifiers)&cmdKey;
  173.             if (isCmdKey!=0L)
  174.                 foundEvent=(thisChar==TheCancelKey);
  175.         }
  176.         if (!foundEvent)
  177.             eventQPtr=(EvQElPtr)(eventQPtr->qLink);
  178.     }
  179.     
  180.     notDoneYet=TRUE;
  181.     do
  182.     {
  183.         if (WaitNextEvent(everyEvent, &event, gIsInBackground ? 100 : 0, 0L))
  184.             DispatchEvents(event);
  185.         else
  186.             notDoneYet=FALSE;
  187.         if (event.what==nullEvent)
  188.             notDoneYet=FALSE;
  189.     }
  190.     while (notDoneYet);
  191.     
  192.     return !foundEvent;
  193. }
  194.